#!/bin/bash


echo "Defining Parameters..."
cat parameters.dat | grep xsize > temp
sed -i "s/\txsize//g" temp
xsize=$(sed "s/\#//g" temp)
echo "...xsize=$xsize"
cat parameters.dat | grep ysize > temp
sed -i "s/\tysize//g" temp
ysize=$(sed "s/\#//g" temp)
echo "...ysize=$ysize"
cat parameters.dat | grep mintl > temp
sed -i "s/\tmintl//g" temp
mintl=$(sed "s/\#//g" temp)
echo "...mintl=$mintl"
cat parameters.dat | grep maxtl > temp
sed -i "s/\tmaxtl//g" temp
maxtl=$(sed "s/\#//g" temp)
echo "...maxtl=$maxtl"
cat parameters.dat | grep mineh > temp
sed -i "s/\tmineh//g" temp
mineh=$(sed "s/\#//g" temp)
echo "...mineh=$mineh"
cat parameters.dat | grep maxeh > temp
sed -i "s/\tmaxeh//g" temp
maxeh=$(sed "s/\#//g" temp)
echo "...maxeh=$maxeh"
cat parameters.dat | grep mindh > temp
sed -i "s/\tmindh//g" temp
mindh=$(sed "s/\#//g" temp)
echo "...mindh=$mindh"
cat parameters.dat | grep maxdh > temp
sed -i "s/\tmaxdh//g" temp
maxdh=$(sed "s/\#//g" temp)
echo "...maxdh=$maxdh"
cat parameters.dat | grep minaf > temp
sed -i "s/\tminaf//g" temp
minaf=$(sed "s/\#//g" temp)
echo "...minaf=$minaf"
cat parameters.dat | grep maxaf > temp
sed -i "s/\tmaxaf//g" temp
maxaf=$(sed "s/\#//g" temp)
echo "...maxaf=$maxaf"
cat parameters.dat | grep minvf > temp
sed -i "s/\tminvf//g" temp
minvf=$(sed "s/\#//g" temp)
echo "...minvf=$minvf"
cat parameters.dat | grep maxvf > temp
sed -i "s/\tmaxvf//g" temp
maxvf=$(sed "s/\#//g" temp)
echo "...maxvf=$maxvf"
cat parameters.dat | grep minsb > temp
sed -i "s/\tminsb//g" temp
minsb=$(sed "s/\#//g" temp)
echo "...minsb=$minsb"
cat parameters.dat | grep maxsb > temp
sed -i "s/\tmaxsb//g" temp
maxsb=$(sed "s/\#//g" temp)
echo "...maxsb=$maxsb"

mkdir data/local_transport_length
mkdir data/local_eroded_hight
mkdir data/local_deposited_hight
mkdir data/local_aeolian_flux
mkdir data/local_avalanche_flux
mkdir data/local_sediment_balance


echo "Plotting local_transport_length for time steps..."
for i in data/raw/local_transport_length*.dat; do

#echo "$(basename $i .dat)"
   	gnuplot << EOF
   	set terminal png
   	set output "data/local_transport_length/pic-$(basename $i .dat).png"
   	unset key
	set grid 
  	set size ratio -1


   	set cbrange [$mintl:$maxtl]
   	set xrange [0:$xsize]
   	set yrange [0:$ysize]
   	plot "$i" binary array=($xsize,$ysize) format="%float" with image
EOF
done

mencoder mf://data/local_transport_length/pic-hight*.png -mf fps=5:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2 -o data/mov/local_transport_length.avi >/dev/null

echo "Plotting local_eroded_hight for time steps..."
for i in data/raw/local_eroded_hight*.dat; do

#echo "$(basename $i .dat)"
   	gnuplot << EOF
   	set terminal png
   	set output "data/local_eroded_hight/pic-$(basename $i .dat).png"
   	unset key
	set grid 
  	set size ratio -1


   	set cbrange [$mineht:$maxeh]
   	set xrange [0:$xsize]
   	set yrange [0:$ysize]
   	plot "$i" binary array=($xsize,$ysize) format="%float" with image
EOF
done

mencoder mf://data/local_eroded_hight/pic-hight*.png -mf fps=5:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2 -o data/mov/local_eroded_hight.avi >/dev/null

echo "Plotting local_transport_length for time steps..."
for i in data/raw/local_deposited_hight*.dat; do

#echo "$(basename $i .dat)"
   	gnuplot << EOF
   	set terminal png
   	set output "data/local_deposited_hight/pic-$(basename $i .dat).png"
   	unset key
	set grid 
  	set size ratio -1


   	set cbrange [$mindh:$maxdh]
   	set xrange [0:$xsize]
   	set yrange [0:$ysize]
   	plot "$i" binary array=($xsize,$ysize) format="%float" with image
EOF
done

mencoder mf://data/local_deposited_hight/pic-hight*.png -mf fps=5:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2 -o data/mov/local_deposited_hight.avi >/dev/null

echo "Plotting local_aeolian_flux for time steps..."
for i in data/raw/local_aeolian_flux*.dat; do

#echo "$(basename $i .dat)"
   	gnuplot << EOF
   	set terminal png
   	set output "data/local_aeolian_flux/pic-$(basename $i .dat).png"
   	unset key
	set grid 
  	set size ratio -1


   	set cbrange [$minaf:$maxaf]
   	set xrange [0:$xsize]
   	set yrange [0:$ysize]
   	plot "$i" binary array=($xsize,$ysize) format="%float" with image
EOF
done

mencoder mf://data/local_aeolian_flux/pic-hight*.png -mf fps=5:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2 -o data/mov/local_aeolian_flux.avi >/dev/null

echo "Plotting local_avalanche_flux for time steps..."
for i in data/raw/local_avalanche_flux*.dat; do

#echo "$(basename $i .dat)"
   	gnuplot << EOF
   	set terminal png
   	set output "data/local_avalanche_flux/pic-$(basename $i .dat).png"
   	unset key
	set grid 
  	set size ratio -1


   	set cbrange [$minvf:$maxvf]
   	set xrange [0:$xsize]
   	set yrange [0:$ysize]
   	plot "$i" binary array=($xsize,$ysize) format="%float" with image
EOF
done

mencoder mf://data/local_avalanche_flux/pic-hight*.png -mf fps=5:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2 -o data/mov/local_avalanche_flux.avi >/dev/null

echo "Plotting local_sediment_balance for time steps..."
for i in data/raw/local_sediment_balance*.dat; do

#echo "$(basename $i .dat)"
   	gnuplot << EOF
   	set terminal png
   	set output "data/local_sediment_balance/pic-$(basename $i .dat).png"
   	unset key
	set grid 
  	set size ratio -1


   	set cbrange [$minsb:$maxsb]
   	set xrange [0:$xsize]
   	set yrange [0:$ysize]
   	plot "$i" binary array=($xsize,$ysize) format="%float" with image
EOF
done

mencoder mf://data/local_sediment_balance/pic-hight*.png -mf fps=5:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2 -o data/mov/local_sediment_balance.avi >/dev/null


